package cn.wbu.zyh.controller.user;

import cn.wbu.zyh.dto.*;
import cn.wbu.zyh.result.PageResult;
import cn.wbu.zyh.result.Result;
import cn.wbu.zyh.service.AnnotationService;
import cn.wbu.zyh.service.ArticleService;
import cn.wbu.zyh.service.FavoritesService;
import cn.wbu.zyh.vo.ArticleVO;
import cn.wbu.zyh.vo.FavoritesVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/teamNote")
@Slf4j
public class TeamNoteController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private AnnotationService annotationService;
    @GetMapping("/list")
    @ApiOperation("笔记分页")
    public Result<PageResult> page(TeamArticlePageQueryDTO teamArticlePageQueryDTO){
        log.info("笔记分页查询,参数为：:{}",teamArticlePageQueryDTO);
        PageResult pageResult = articleService.TeamArticlePageQuery(teamArticlePageQueryDTO);
        return Result.success(pageResult);
    }

    @GetMapping("/list/category")
    @ApiOperation("分类分页")
    public Result<PageResult> page(TagPageQueryDTO tagPageQueryDTO){
        log.info("笔记分类查询,参数为：:{}",tagPageQueryDTO);
        PageResult pageResult = articleService.pageQueryByTagId(tagPageQueryDTO);
        return Result.success(pageResult);
    }
    @GetMapping("/{id}")
    @ApiOperation("查询笔记")
    public Result<ArticleVO> getById(@PathVariable Long id){
        log.info("查询笔记信息：{}",id);
        ArticleVO articleVO = articleService.getByIdWithTags(id);
        return Result.success(articleVO);
    }

    @PostMapping("/add")
    @ApiOperation("新增笔记")
    public Result save(@RequestBody ArticleDTO articleDTO) {
        articleService.saveWithTag(articleDTO);
        annotationService.insertAnnotationsBatch(articleDTO);
        return Result.success();
    }

    @PostMapping("/updatefavorite")
    @ApiOperation("新增/删除收藏")
    public Result<FavoritesVO> savefavorite(@RequestBody FavoritesDTO favoritesDTO) {
        FavoritesVO favoritesVO = favoritesService.favorite(favoritesDTO);
        return Result.success(favoritesVO);
    }

    @GetMapping("/check")
    public Result<FavoritesVO> checkFavorite(@RequestParam String username, @RequestParam Long articleId) {
        boolean isFavorite = favoritesService.isFavorite(username, articleId);
        FavoritesVO favoritesVO = new FavoritesVO();
        favoritesVO.setArticleId(articleId);
        favoritesVO.setUsername(username);
        favoritesVO.setFavorite(isFavorite);
        return Result.success(favoritesVO);
    }
    @PutMapping
    @ApiOperation("修改笔记")
    public Result update(@RequestBody ArticleDTO articleDTO) {
        articleService.update(articleDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除笔记")
    public Result delete(@RequestParam Long id){
        articleService.deleteAticle(id);
        return Result.success();
    }
}
